home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1640UDI (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  12.3 KB  |  420 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.JButton;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JScrollBar;
  6. import com.sun.java.swing.LookAndFeel;
  7. import com.sun.java.swing.SwingConstants;
  8. import com.sun.java.swing.Timer;
  9. import com.sun.java.swing.UIManager;
  10. import com.sun.java.swing.plaf.ComponentUI;
  11. import com.sun.java.swing.plaf.ScrollBarUI;
  12. import java.awt.Color;
  13. import java.awt.Component;
  14. import java.awt.Container;
  15. import java.awt.Dimension;
  16. import java.awt.Graphics;
  17. import java.awt.Insets;
  18. import java.awt.LayoutManager;
  19. import java.awt.Rectangle;
  20. import java.io.Serializable;
  21.  
  22. public class BasicScrollBarUI extends ScrollBarUI implements LayoutManager, Serializable, SwingConstants {
  23.    private static final Dimension minimumThumbSize = new Dimension(8, 8);
  24.    private static final Dimension maximumThumbSize = new Dimension(4096, 4096);
  25.    private static Color thumbHighlightColor;
  26.    private static Color thumbLightShadowColor;
  27.    private static Color thumbDarkShadowColor;
  28.    private static Color thumbColor;
  29.    private static Color trackColor;
  30.    private static Color trackHighlightColor;
  31.    private static boolean scrollBarColorsInitialized = false;
  32.    protected JScrollBar scrollbar;
  33.    protected JButton incrButton;
  34.    protected JButton decrButton;
  35.    protected boolean isDragging;
  36.    protected TrackListener trackListener;
  37.    protected ArrowButtonListener buttonListener;
  38.    protected ModelListener modelListener;
  39.    private Rectangle thumbRect;
  40.    private Rectangle trackRect;
  41.    protected int trackHighlight;
  42.    protected static final int NO_HIGHLIGHT = 0;
  43.    protected static final int DECREASE_HIGHLIGHT = 1;
  44.    protected static final int INCREASE_HIGHLIGHT = 2;
  45.    protected ScrollListener scrollListener;
  46.    protected Timer scrollTimer;
  47.  
  48.    public void addLayoutComponent(String name, Component child) {
  49.    }
  50.  
  51.    protected void configureScrollBarColors() {
  52.       if (!scrollBarColorsInitialized) {
  53.          thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
  54.          thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbLightShadow");
  55.          thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow");
  56.          thumbColor = UIManager.getColor("ScrollBar.thumb");
  57.          trackColor = UIManager.getColor("ScrollBar.track");
  58.          trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
  59.          scrollBarColorsInitialized = true;
  60.       }
  61.  
  62.    }
  63.  
  64.    protected JButton createDecreaseButton(int orientation) {
  65.       return new BasicArrowButton(orientation);
  66.    }
  67.  
  68.    protected JButton createIncreaseButton(int orientation) {
  69.       return new BasicArrowButton(orientation);
  70.    }
  71.  
  72.    public static ComponentUI createUI(JComponent c) {
  73.       return new BasicScrollBarUI();
  74.    }
  75.  
  76.    public Dimension getMaximumSize(JComponent c) {
  77.       return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  78.    }
  79.  
  80.    protected Dimension getMaximumThumbSize() {
  81.       return maximumThumbSize;
  82.    }
  83.  
  84.    public Dimension getMinimumSize(JComponent c) {
  85.       return this.getPreferredSize(c);
  86.    }
  87.  
  88.    protected Dimension getMinimumThumbSize() {
  89.       return minimumThumbSize;
  90.    }
  91.  
  92.    public Dimension getPreferredSize(JComponent c) {
  93.       return this.scrollbar.getOrientation() == 1 ? new Dimension(16, 48) : new Dimension(48, 16);
  94.    }
  95.  
  96.    protected Rectangle getThumbBounds() {
  97.       return this.thumbRect;
  98.    }
  99.  
  100.    protected Rectangle getTrackBounds() {
  101.       return this.trackRect;
  102.    }
  103.  
  104.    public void installUI(JComponent c) {
  105.       this.scrollbar = (JScrollBar)c;
  106.       this.thumbRect = new Rectangle(0, 0, 0, 0);
  107.       this.trackRect = new Rectangle(0, 0, 0, 0);
  108.       this.trackHighlight = 0;
  109.       switch (((JScrollBar)c).getOrientation()) {
  110.          case 0:
  111.             this.incrButton = this.createIncreaseButton(3);
  112.             this.decrButton = this.createDecreaseButton(7);
  113.             break;
  114.          case 1:
  115.             this.incrButton = this.createIncreaseButton(5);
  116.             this.decrButton = this.createDecreaseButton(1);
  117.       }
  118.  
  119.       this.scrollbar.setLayout(this);
  120.       this.scrollbar.add(this.incrButton);
  121.       this.scrollbar.add(this.decrButton);
  122.       this.trackListener = new TrackListener(this);
  123.       this.buttonListener = new ArrowButtonListener(this);
  124.       this.modelListener = new ModelListener(this);
  125.       this.scrollbar.addMouseListener(this.trackListener);
  126.       this.scrollbar.addMouseMotionListener(this.trackListener);
  127.       this.scrollbar.getModel().addChangeListener(this.modelListener);
  128.       if (this.incrButton != null) {
  129.          this.incrButton.addMouseListener(this.buttonListener);
  130.       }
  131.  
  132.       if (this.decrButton != null) {
  133.          this.decrButton.addMouseListener(this.buttonListener);
  134.       }
  135.  
  136.       this.scrollbar.setEnabled(this.scrollbar.isEnabled());
  137.       this.scrollbar.setOpaque(true);
  138.       this.scrollListener = new ScrollListener(this);
  139.       this.scrollTimer = new Timer(100, this.scrollListener);
  140.       this.scrollTimer.setInitialDelay(300);
  141.       LookAndFeel.installBorder(this.scrollbar, "ScrollBar.border");
  142.       this.configureScrollBarColors();
  143.    }
  144.  
  145.    public void layoutContainer(Container scrollbarContainer) {
  146.       if (!this.isDragging) {
  147.          JScrollBar scrollbar = (JScrollBar)scrollbarContainer;
  148.          switch (scrollbar.getOrientation()) {
  149.             case 0:
  150.                this.layoutHScrollbar(scrollbar);
  151.                break;
  152.             case 1:
  153.                this.layoutVScrollbar(scrollbar);
  154.          }
  155.  
  156.       }
  157.    }
  158.  
  159.    protected void layoutHScrollbar(JScrollBar sb) {
  160.       Dimension sbSize = ((Component)sb).getSize();
  161.       Insets sbInsets = ((JComponent)sb).getInsets();
  162.       int itemH = sbSize.height - (sbInsets.top + sbInsets.bottom);
  163.       int itemY = sbInsets.top;
  164.       int decrButtonW = this.decrButton.getPreferredSize().width;
  165.       int decrButtonX = sbInsets.left;
  166.       int incrButtonW = this.incrButton.getPreferredSize().width;
  167.       int incrButtonX = sbSize.width - (sbInsets.right + incrButtonW);
  168.       int sbInsetsW = sbInsets.left + sbInsets.right;
  169.       int sbButtonsW = decrButtonW + incrButtonW;
  170.       float trackW = (float)(sbSize.width - (sbInsetsW + sbButtonsW));
  171.       float min = (float)sb.getMinimum();
  172.       float extent = (float)sb.getVisibleAmount();
  173.       float range = (float)sb.getMaximum() - min;
  174.       float value = (float)sb.getValue();
  175.       int thumbW = range <= 0.0F ? this.getMaximumThumbSize().width : (int)(trackW * (extent / range));
  176.       thumbW = Math.max(thumbW, this.getMinimumThumbSize().width);
  177.       thumbW = Math.min(thumbW, this.getMaximumThumbSize().width);
  178.       int thumbX = incrButtonX - thumbW;
  179.       if (sb.getValue() < sb.getMaximum() - sb.getVisibleAmount()) {
  180.          float thumbRange = trackW - (float)thumbW;
  181.          thumbX = (int)(0.5F + thumbRange * ((value - min) / (range - extent)));
  182.          thumbX += decrButtonX + decrButtonW;
  183.       }
  184.  
  185.       int sbAvailButtonW = sbSize.width - sbInsetsW;
  186.       if (sbAvailButtonW < sbButtonsW) {
  187.          incrButtonW = decrButtonW = sbAvailButtonW / 2;
  188.          incrButtonX = sbSize.width - (sbInsets.right + incrButtonW);
  189.       }
  190.  
  191.       this.decrButton.setBounds(decrButtonX, itemY, decrButtonW, itemH);
  192.       this.incrButton.setBounds(incrButtonX, itemY, incrButtonW, itemH);
  193.       int itrackX = decrButtonX + decrButtonW;
  194.       int itrackW = incrButtonX - itrackX;
  195.       this.trackRect.setBounds(itrackX, itemY, itrackW, itemH);
  196.       if (thumbW >= (int)trackW) {
  197.          this.setThumbBounds(0, 0, 0, 0);
  198.       } else {
  199.          if (thumbX + thumbW > incrButtonX) {
  200.             thumbX = incrButtonX - thumbW;
  201.          }
  202.  
  203.          if (thumbX < decrButtonX + decrButtonW) {
  204.             thumbX = decrButtonX + decrButtonW + 1;
  205.          }
  206.  
  207.          this.setThumbBounds(thumbX, itemY, thumbW, itemH);
  208.       }
  209.  
  210.    }
  211.  
  212.    protected void layoutVScrollbar(JScrollBar sb) {
  213.       Dimension sbSize = ((Component)sb).getSize();
  214.       Insets sbInsets = ((JComponent)sb).getInsets();
  215.       int itemW = sbSize.width - (sbInsets.left + sbInsets.right);
  216.       int itemX = sbInsets.left;
  217.       int decrButtonH = this.decrButton.getPreferredSize().height;
  218.       int decrButtonY = sbInsets.top;
  219.       int incrButtonH = this.incrButton.getPreferredSize().height;
  220.       int incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH);
  221.       int sbInsetsH = sbInsets.top + sbInsets.bottom;
  222.       int sbButtonsH = decrButtonH + incrButtonH;
  223.       float trackH = (float)(sbSize.height - (sbInsetsH + sbButtonsH));
  224.       float min = (float)sb.getMinimum();
  225.       float extent = (float)sb.getVisibleAmount();
  226.       float range = (float)sb.getMaximum() - min;
  227.       float value = (float)sb.getValue();
  228.       int thumbH = range <= 0.0F ? this.getMaximumThumbSize().height : (int)(trackH * (extent / range));
  229.       thumbH = Math.max(thumbH, this.getMinimumThumbSize().height);
  230.       thumbH = Math.min(thumbH, this.getMaximumThumbSize().height);
  231.       int thumbY = incrButtonY - thumbH;
  232.       if (sb.getValue() < sb.getMaximum() - sb.getVisibleAmount()) {
  233.          float thumbRange = trackH - (float)thumbH;
  234.          thumbY = (int)(0.5F + thumbRange * ((value - min) / (range - extent)));
  235.          thumbY += decrButtonY + decrButtonH;
  236.       }
  237.  
  238.       int sbAvailButtonH = sbSize.height - sbInsetsH;
  239.       if (sbAvailButtonH < sbButtonsH) {
  240.          incrButtonH = decrButtonH = sbAvailButtonH / 2;
  241.          incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH);
  242.       }
  243.  
  244.       this.decrButton.setBounds(itemX, decrButtonY, itemW, decrButtonH);
  245.       this.incrButton.setBounds(itemX, incrButtonY, itemW, incrButtonH);
  246.       int itrackY = decrButtonY + decrButtonH;
  247.       int itrackH = incrButtonY - itrackY;
  248.       this.trackRect.setBounds(itemX, itrackY, itemW, itrackH);
  249.       if (thumbH >= (int)trackH) {
  250.          this.setThumbBounds(0, 0, 0, 0);
  251.       } else {
  252.          if (thumbY + thumbH > incrButtonY) {
  253.             thumbY = incrButtonY - thumbH;
  254.          }
  255.  
  256.          if (thumbY < decrButtonY + decrButtonH) {
  257.             thumbY = decrButtonY + decrButtonH + 1;
  258.          }
  259.  
  260.          this.setThumbBounds(itemX, thumbY, itemW, thumbH);
  261.       }
  262.  
  263.    }
  264.  
  265.    public Dimension minimumLayoutSize(Container scrollbarContainer) {
  266.       return this.getMinimumSize((JComponent)scrollbarContainer);
  267.    }
  268.  
  269.    public void paint(Graphics g, JComponent c) {
  270.       this.paintTrack(g, c, this.getTrackBounds());
  271.       this.paintThumb(g, c, this.getThumbBounds());
  272.    }
  273.  
  274.    protected void paintDecreaseHighlight(Graphics g) {
  275.       Insets insets = this.scrollbar.getInsets();
  276.       Rectangle thumbR = this.getThumbBounds();
  277.       g.setColor(trackHighlightColor);
  278.       if (this.scrollbar.getOrientation() == 1) {
  279.          int x = insets.left;
  280.          int y = this.decrButton.getY() + this.decrButton.getHeight();
  281.          int w = this.scrollbar.getWidth() - (insets.left + insets.right);
  282.          int h = thumbR.y - y;
  283.          g.fillRect(x, y, w, h);
  284.       } else {
  285.          int x = this.decrButton.getX() + this.decrButton.getHeight();
  286.          int y = insets.top;
  287.          int w = thumbR.x - x;
  288.          int h = this.scrollbar.getHeight() - (insets.top + insets.bottom);
  289.          g.fillRect(x, y, w, h);
  290.       }
  291.  
  292.    }
  293.  
  294.    protected void paintIncreaseHighlight(Graphics g) {
  295.       Insets insets = this.scrollbar.getInsets();
  296.       Rectangle thumbR = this.getThumbBounds();
  297.       g.setColor(trackHighlightColor);
  298.       if (this.scrollbar.getOrientation() == 1) {
  299.          int x = insets.left;
  300.          int y = thumbR.y + thumbR.height;
  301.          int w = this.scrollbar.getWidth() - (insets.left + insets.right);
  302.          int h = this.incrButton.getY() - y;
  303.          g.fillRect(x, y, w, h);
  304.       } else {
  305.          int x = thumbR.x + thumbR.width;
  306.          int y = insets.top;
  307.          int w = this.incrButton.getX() - x;
  308.          int h = this.scrollbar.getHeight() - (insets.top + insets.bottom);
  309.          g.fillRect(x, y, w, h);
  310.       }
  311.  
  312.    }
  313.  
  314.    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
  315.       if (!thumbBounds.isEmpty() && this.scrollbar.isEnabled()) {
  316.          int w = thumbBounds.width;
  317.          int h = thumbBounds.height;
  318.          g.translate(thumbBounds.x, thumbBounds.y);
  319.          g.setColor(thumbDarkShadowColor);
  320.          g.drawRect(0, 0, w - 1, h - 1);
  321.          g.setColor(thumbColor);
  322.          g.fillRect(0, 0, w - 1, h - 1);
  323.          g.setColor(thumbHighlightColor);
  324.          g.drawLine(1, 1, 1, h - 2);
  325.          g.drawLine(2, 1, w - 3, 1);
  326.          g.setColor(thumbLightShadowColor);
  327.          g.drawLine(2, h - 2, w - 2, h - 2);
  328.          g.drawLine(w - 2, 1, w - 2, h - 3);
  329.          g.translate(-thumbBounds.x, -thumbBounds.y);
  330.       }
  331.    }
  332.  
  333.    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
  334.       g.setColor(trackColor);
  335.       g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
  336.       if (this.trackHighlight == 1) {
  337.          this.paintDecreaseHighlight(g);
  338.       } else if (this.trackHighlight == 2) {
  339.          this.paintIncreaseHighlight(g);
  340.       }
  341.  
  342.    }
  343.  
  344.    public Dimension preferredLayoutSize(Container scrollbarContainer) {
  345.       return this.getPreferredSize((JComponent)scrollbarContainer);
  346.    }
  347.  
  348.    public void removeLayoutComponent(Component child) {
  349.    }
  350.  
  351.    protected void scrollByBlock(int direction) {
  352.       synchronized(this.scrollbar){}
  353.  
  354.       try {
  355.          int oldValue = this.scrollbar.getValue();
  356.          int blockIncrement = this.scrollbar.getBlockIncrement(direction);
  357.          int delta = blockIncrement * (direction > 0 ? 1 : -1);
  358.          this.scrollbar.setValue(oldValue + delta);
  359.          this.trackHighlight = direction > 0 ? 2 : 1;
  360.          Rectangle dirtyRect = this.getTrackBounds();
  361.          this.scrollbar.repaint(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height);
  362.       } catch (Throwable var8) {
  363.          throw var8;
  364.       }
  365.  
  366.    }
  367.  
  368.    protected void scrollByUnit(int direction) {
  369.       synchronized(this.scrollbar){}
  370.  
  371.       try {
  372.          int delta;
  373.          if (direction > 0) {
  374.             delta = this.scrollbar.getUnitIncrement(direction);
  375.          } else {
  376.             delta = -this.scrollbar.getUnitIncrement(direction);
  377.          }
  378.  
  379.          this.scrollbar.setValue(delta + this.scrollbar.getValue());
  380.       } catch (Throwable var5) {
  381.          throw var5;
  382.       }
  383.  
  384.    }
  385.  
  386.    protected void setThumbBounds(int x, int y, int width, int height) {
  387.       if (this.thumbRect.x != x || this.thumbRect.y != y || this.thumbRect.width != width || this.thumbRect.height != height) {
  388.          int minX = Math.min(x, this.thumbRect.x);
  389.          int minY = Math.min(y, this.thumbRect.y);
  390.          int maxX = Math.max(x + width, this.thumbRect.x + this.thumbRect.width);
  391.          int maxY = Math.max(y + height, this.thumbRect.y + this.thumbRect.height);
  392.          this.thumbRect.setBounds(x, y, width, height);
  393.          this.scrollbar.repaint(minX, minY, maxX - minX, maxY - minY);
  394.       }
  395.    }
  396.  
  397.    public void uninstallUI(JComponent c) {
  398.       this.scrollTimer.stop();
  399.       this.scrollTimer = null;
  400.       if (this.decrButton != null) {
  401.          this.decrButton.removeMouseListener(this.buttonListener);
  402.       }
  403.  
  404.       if (this.incrButton != null) {
  405.          this.incrButton.removeMouseListener(this.buttonListener);
  406.       }
  407.  
  408.       this.scrollbar.getModel().removeChangeListener(this.modelListener);
  409.       this.scrollbar.removeMouseListener(this.trackListener);
  410.       this.scrollbar.removeMouseMotionListener(this.trackListener);
  411.       this.scrollbar.remove(this.incrButton);
  412.       this.scrollbar.remove(this.decrButton);
  413.       this.scrollbar.setLayout((LayoutManager)null);
  414.       this.thumbRect = null;
  415.       this.incrButton = null;
  416.       this.decrButton = null;
  417.       this.scrollbar = null;
  418.    }
  419. }
  420.